package com.baicaoyuan.web.service;

import com.baicaoyuan.web.enums.CrowdfundingStatusEnum;
import com.baicaoyuan.web.model.CrowdfundingDO;
import com.baicaoyuan.web.repository.CrowdfundingRepository;
import com.baicaoyuan.web.request.br.BCrowdfundingEditRequest;
import com.baicaoyuan.web.request.br.BCrowdfundingIdRequest;
import com.baicaoyuan.web.request.br.BCrowdfundingIdStatusRequest;
import com.baicaoyuan.web.request.br.BCrowdfundingRequest;
import com.baicaoyuan.web.response.CrowdfundingResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述：
 *
 * @author 罗富祥
 * 创建时间： 2018/1/4
 */
@Service
public class CrowdfundingService {

    @Autowired
    CrowdfundingRepository crowdfundingRepository;

    public void save(BCrowdfundingRequest request) {
        Objects.requireNonNull(request);

        CrowdfundingDO c = new CrowdfundingDO();
        BeanUtils.copyProperties(request, c);
        c.setStatus(CrowdfundingStatusEnum.INIT.getStatus());
        c.setJoinNumber(0L);
        this.crowdfundingRepository.save(c);
    }

    public void edit(BCrowdfundingEditRequest request) {
        Objects.requireNonNull(request);

        CrowdfundingDO c = crowdfundingRepository.findOne(request.getId());
        BeanUtils.copyProperties(request, c);
        this.crowdfundingRepository.save(c);
    }

    public CrowdfundingResponse findOne(BCrowdfundingIdRequest request) {
        CrowdfundingDO crowdfundingDO = this.crowdfundingRepository.findOne(request.getId());
        CrowdfundingResponse response = new CrowdfundingResponse();
        BeanUtils.copyProperties(crowdfundingDO, response);
        return response;
    }

    public boolean delete(BCrowdfundingIdRequest request) {
        CrowdfundingDO crowdfundingDO = this.crowdfundingRepository.findOne(request.getId());
        crowdfundingDO.setStatus(CrowdfundingStatusEnum.DELETE.getStatus());
        return this.crowdfundingRepository.save(crowdfundingDO) != null;
    }

    public boolean close(BCrowdfundingIdRequest request) {
        CrowdfundingDO crowdfundingDO = this.crowdfundingRepository.findOne(request.getId());
        crowdfundingDO.setStatus(CrowdfundingStatusEnum.CLOSE.getStatus());
        return this.crowdfundingRepository.save(crowdfundingDO) != null;
    }

    public boolean updateStatus(BCrowdfundingIdStatusRequest request) {
        CrowdfundingDO crowdfundingDO = this.crowdfundingRepository.findOne(request.getId());
        crowdfundingDO.setStatus(request.getStatus());
        return this.crowdfundingRepository.save(crowdfundingDO) != null;
    }


    public Integer getCountOfAll(List<String> status) {
        return this.crowdfundingRepository.countAllByStatusNotIn(status);
    }

    public List<CrowdfundingResponse> getCrowdfundings(List<String> status, Integer page, Integer size) {

        Sort sort = new Sort(Sort.Direction.DESC, "sort");
        Pageable pageable = new PageRequest(page - 1, size, sort);

        List<CrowdfundingDO> list = this.crowdfundingRepository.findAllByStatusNotIn(status, pageable);

        List<CrowdfundingResponse> result = new ArrayList<>();
        list.stream().forEach(x -> {
            CrowdfundingResponse r = new CrowdfundingResponse();
            BeanUtils.copyProperties(x, r);
            result.add(r);
        });

        return result;
    }

}

